package client.gui;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Scanner;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTextField;


import client.CheckinClient;
import client.Service;

public class CheckInPanel extends DisplayPanel implements ActionListener, MouseListener {
	private static final long serialVersionUID = 1L;
	private final JButton Register = new JButton("Register for flight");
	private final JTextField RegNum = new JTextField();
	private JLabel temp = new JLabel("- Please enter your registration number to check-in -");
	private JLabel timeInfo = new JLabel("-Time remaining to departure:-");
	private JLabel Name = new JLabel("Name: - ");
	private JLabel airline = new JLabel("Airline: - ");
	private JLabel gate = new JLabel("Gate: - ");
	private long count;
	private JLabel timer = new JLabel("HH:MM");
	private javax.swing.Timer t = new javax.swing.Timer(1000,this);
	
	Service serv;
	public CheckInPanel(Service s) {
		super(s);
		this.setLayout(null);
		init();
	}
	public void init(){
		ImageIcon icon = new ImageIcon("clock.png");
		JLabel image = new JLabel();
		image.setIcon(icon);
		add(image);
		image.setBounds(400,250,312,281);
		add(Register);
		Register.addActionListener(this);
		Register.setBounds(350,35,200,20);
		add(RegNum);
		RegNum.addActionListener(this);
		RegNum.setBounds(35,35,300,20);
		temp.setFont(new Font("Times New Roman",1,15));
		this.add(temp);
		temp.setBounds(40,70,700,20);
		timer.setFont(new Font("Times New Roman",1,70));
		this.add(timer);
		timer.setBounds(450, 30, 350, 250);
		this.add(timeInfo);
		timeInfo.setBounds(450,200,200,20);
		this.add(Name);
		Name.setFont(new Font("Times New Roman",1,15));
		Name.setBounds(40,100,200,20);
		this.add(airline);
		airline.setFont(new Font("Times New Roman",1,15));
		airline.setBounds(40,120,200,20);
		this.add(gate);
		gate.setFont(new Font("Times New Roman",1,15));
		gate.setBounds(40,140,200,20);
	}
	public void timerGO(){
		long hours = count/(60*60*1000);
		long minutes = ((count/(60*1000))-(hours*60));
		System.out.println(minutes);
		if(minutes < 10){
			timer.setText(hours+":0"+minutes);
		}else{
			timer.setText(hours+":"+minutes);
		}
		t.start();
	}
	public void timerStop(){
		t.stop();
	}
	public void out(String g,String n,String air,String detail){
		gate.setText("Gate: "+g);
		Name.setText("Name: "+n);
		airline.setText("Airline: "+air);
	}
	public void outReset(){
		gate.setText("Gate: - ");
		Name.setText("Name: - ");
		airline.setText("Airline: - ");
	}
	public void actionPerformed(ActionEvent e) {
		if(e.getSource()==Register){
			String t = ((CheckinClient)myClient).checkIn(Integer.parseInt(RegNum.getText()));
			Scanner s = new Scanner(t);
			String eval = s.next();
			if(eval.equals("Yes")){
				timerStop();
				temp.setText("You are registered");
				count=Integer.parseInt(s.next());
				timerGO();
				out(s.next(),s.next(),s.next(),s.next());
			}else if(eval.equals("No")){
				timerStop();
				temp.setText("You have already registered");
				count=Integer.parseInt(s.next());
				timerGO();
				out(s.next(),s.next(),s.next(),s.next());
			}else{
				timerStop();
				outReset();
				timer.setText("HH:MM");
				temp.setText(t);
			}
		}else{
			long hours = count/(60*60*1000);
			long minutes = ((count/(60*1000))-((hours)*60));
			if(minutes < 10){
				timer.setText(hours+":0"+minutes);
			}else{
				timer.setText(hours+":"+minutes);
			}
			count -= 1000;
		}
		this.repaint();
	}

	public void mouseClicked(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

}
